package com.voltbarn.vbsensors;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Message implements Comparable<Message>{
	static SimpleDateFormat FORMATTER = 
		new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
	private String ID;
	private URL link;
	private String temperature;
	private String t0;
	private String t1;
	private String pressure;
	private String winds;
	private String humidity;
	private String dates;
	private String rain;
	private String rain24;

	public String getID() {
		return ID;
	}

	public void setID(String ID) {
		this.ID = ID.trim();
	}
	
	public String getPressure() {
		return pressure;
	}

	public void setPressure(String pressure) {
		this.pressure = pressure.trim();
	}	
	
	public String getWinds() {
		return (winds != "" ? winds : null);
	}

	public void setWinds(String winds) {
		this.winds = winds.trim();
	}	

	// getters and setters omitted for brevity 
	public URL getLink() {
		return link;
	}
	
	public void setLink(String link) {
		try {
			this.link = new URL(link);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	public String getTemperature() {
		return temperature;
	}

	public void setTemperature(String temperature) {
		this.temperature = temperature.trim();
	}

	public String getTemp0() {
		return t0;
	}

	public void setTemp0(String t0) {
		this.t0 = t0.trim();
	}
	
	public String getTemp1() {
		return t1;
	}

	public void setTemp1(String t1) {
		this.t1 = t1.trim();
	}
	
	public String getHumidity() {
		if (humidity.equals("-999"))
				return "N/A";
		else
			return humidity;
	}
	
	public void setHumidity(String humidity) {
		this.humidity = humidity.trim();
	}
	
	public String getRain() {
		return (rain.equals("") ? null : (rain.equals("-999.00") ? "N/A" : rain));

	}
	
	public void setRain(String rain) {
		this.rain = rain.trim();
	}
	
	public String getRain24() {	
		return (rain24.equals("") ? null : (rain24.equals("-999.00") ? "N/A" : rain24));
	}
	
	public void setRain24(String rain24) {
		this.rain24 = rain24.trim();
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates.trim();
	}
	
	
	public Message copy(){
		Message copy = new Message();
		copy.ID = ID;
		copy.link = link;
		copy.temperature = temperature;
		copy.t0 = t0;
		copy.t1 = t1;
		copy.pressure = pressure;
		copy.winds = winds;
		copy.dates = dates;
		copy.humidity = humidity;
		copy.rain = rain;
		copy.rain24 = rain24;
		return copy;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Station: ");
		sb.append(ID);
		sb.append('\n');
		sb.append("Date: ");
		sb.append(dates);
		sb.append('\n');
		sb.append("Temperature: ");
		sb.append('\n');
		sb.append("Humidity: ");
		sb.append(humidity);
		sb.append('\n');
		sb.append("Pressure: ");
		sb.append(pressure);
		sb.append('\n');
		sb.append("Wind: ");
		sb.append(winds);
		sb.append('\n');	
		sb.append("Link: ");
		sb.append(link);
		sb.append('\n');
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((dates == null) ? 0 : dates.hashCode());
		result = prime * result
				+ ((ID == null) ? 0 : ID.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		result = prime * result + ((ID == null) ? 0 : ID.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (dates == null) {
			if (other.dates != null)
				return false;
		} else if (!dates.equals(other.dates))
			return false;
		if (ID == null) {
			if (other.ID != null)
				return false;
		} else if (!ID.equals(other.ID))
			return false;
		if (link == null) {
			if (other.link != null)
				return false;
		} else if (!link.equals(other.link))
			return false;
		return true;
	}

	public int compareTo(Message another) {
		if (another == null) return 1;
		// sort descending, most recent first
		return another.dates.compareTo(dates);
	}
}
